package zb.ex;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
import java.util.Scanner;

/**
 * @author wzby
 * @Date: 2022/8/9 11:02
 */
/*public class Server {
    public static void main(String[] args) {
        try {
            Server server = new Server(6688);
//accept()将会有一个中断等待
            Socket a = server.accept();
            System.out.println("有人连线");
            new Thread(new Read(a)).start();
            new Thread(new Send(a, "服务器")).start();
        } catch (IOException e) {
            System.out.println("服务器启动失败，端口6688被占用");
        }
    }
}

class Read implements Runnable {
    Socket ss = null;

    public Read(Socket ss) {
        this.ss = ss;
    }

    @Override
    public void run() {
        BufferedReader br = null;
        try {
            br = new BufferedReader(new
                    InputStreamReader(ss.getInputStream()));
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        while (true) {
            String sss = null;
            try {
                sss = br.readLine();
                System.out.println(sss);
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
    }
}

class Send implements Runnable {
    String name = "";
    Socket ss = null;

    public Send(Socket ss, String name) {
        this.ss = ss;
        this.name = name;
    }

    @Override
    public void run() {
        PrintWriter out = null;
        try {
            out = new PrintWriter(ss.getOutputStream());
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        while (true) {
            out.printf("%s:%s\r\n", name, new
                    Scanner(System.in).nextLine());
            out.flush();
        }
    }
}*/